#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <fcntl.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>

#include <netinet/in.h>
#include <arpa/inet.h>

void *workfunc(void *)
{
	int ls_fd = socket(AF_INET, SOCK_STREAM, 0);
	if (-1 == ls_fd)
	{
		perror("create listen socekt");
		return NULL;
	}

	struct sockaddr_in addr;
	addr.sin_family = AF_INET;
	addr.sin_port = htons(10086);
	addr.sin_addr.s_addr =inet_addr("192.168.1.111");

	socklen_t len = sizeof(struct sockaddr);
	int rst = connect(ls_fd, (struct sockaddr *)&addr, len);
	if( -1 == rst)
	{
		perror("connect :");
		return NULL;
	}

	int nTime = 1;
	char szRecvBuf[1024] = {'\0'};
	char szSendBuf[1024] = {'\0'};
	long tid = pthread_self();

	while(1)
	{
		sprintf(szSendBuf, "hello server, this is %ld, this is %d times!", tid, nTime++);
		send(ls_fd, szSendBuf, strlen(szSendBuf), 0);
		recv(ls_fd, szRecvBuf, 1024, 0);
		printf("%s\n", szRecvBuf);
	}

	return  NULL;
}


int main(int argc, char **argv)
{
	int thread_num = 200;
	pthread_t tid[thread_num];

	int i = 0;
	for(; i < thread_num; ++i)
	{
		pthread_create(&tid[i], NULL, workfunc, NULL);
	}
	getchar();
	return 0;
}

